layui.use(['form', 'table','element'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        hidde=true,
        hiddes=false,
        element = layui.element,
        init_data= {
            elem: '#currentTableId1',
            url: '/admin/Ranking/index?action=ajax',
            method: 'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter',],
            cols: [[
                {field: 'ranking_num', title: '名次'},
                {field: 'nickname', title: '名称'},
                {field: 'numbers', title: '数量'},
                {field: 'team_numbers', title: '团队人数',hide:hiddes},
                {field: 'thumb', title: '头像',toolbar: '#thumbTpl',hide:hidde},
                {field: 'category', title: '类型', sort: true},
                {title: '操作', toolbar: '#delTpl', minWidth: 120,sort: true}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        };


    table.render(init_data);

    element.on('tab(test)', function(elem){
        let id=parseInt(elem.index)+1

        if(id!=1)
        {
            hidde=false;
            hiddes=true;
        }else{
            hidde=true;
            hiddes=false;
        }

        init_data.cols[0][3].hide=hiddes
        init_data.cols[0][4].hide=hidde
        init_data.elem='#currentTableId'+id
        init_data.url='/admin/Ranking/index?action=ajax&category='+id
        table.render(init_data);
    });


    table.on('toolbar(currentTableFilter)', function (obj) {

        if (obj.event === 'add') {  // 监听添加操作
            let index = layer.open({
                title: '添加排行',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Ranking/Saveinfo',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }



    });

    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        let code=data.member_code

        //假数据

        if(data.cate==1)
        {
            code= data.id;
        }else{
            code=data.fake_id
        }

        if (obj.event === 'delete') {



            layer.confirm('确认删除么？', function (index) {

                $.ajax({
                    url: '/admin/Ranking/DelInfo',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: code,
                        type:data.cate
                    },
                    success: function(data) {
                        if (data.status) {
                            layer.msg('删除成功!', {icon: 1, time: 1000},function () {
                                $(obj)[0].tr.remove()
                                // location.reload()
                            });

                        }else{
                            layer.msg(data.msg, {
                                icon: 2,
                                time: 3000
                            });

                        }

                    },
                    error: function(re) {

                        layer.open({
                            title: '删除失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });
                // obj.del();
                // layer.close(index);
            });
        }

        if(obj.event === 'thumb'){

            $.getJSON('/admin/Ranking/GetThumb?code='+code,function(json){
                if(!json.status)
                {
                    layer.open({
                        title: '错误信息',
                        content: '<span style="color:red">'+json.code+':</span>'+json.msg
                    })
                    return false
                }
                let aa={
                    "title": data.nickname, //相册标题
                    "id": 1, //相册id
                    "start": 0, //初始显示的图片序号，默认0
                    "data": [   //相册包含的图片，数组格式
                        {
                            "alt": data.nickname,
                            "pid": 2, //图片id
                            "src": json.data, //原图地址
                            "thumb": json.data //缩略图地址
                        }
                    ]
                }
                layer.photos({
                    photos: aa
                    ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            });
        }

        if (obj.event === 'edit') {


            let index = layer.open({
                title: '排行详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Ranking/Saveinfo?category='+data.cate+'&id='+code,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }
    });






});